<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>导演管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增导演</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 分割线 -->
    <el-divider></el-divider>
    <!-- 添加导演表单 -->
    <el-form :model="form" label-width="80px">
      <el-form-item label="导演姓名">
        <el-input style="width: 300px" v-model="form.directorName"></el-input>
      </el-form-item>
      <el-form-item label="导演头像">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:9000/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <el-image
            v-if="form.directorAvatar"
            :src="form.directorAvatar"
            class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即新增</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        directorName: "",
        directorAvatar: "",
      },
    }
  },
  methods: {
    onSubmit() {
      // 发送post请求
      this.$http.DirectorApi.add(this.form).then((res) => {
        console.log(res)
        if (res.data.code == 200) {
          this.$message.success("添加成功")
          this.$router.push("/home/director-list")
        }
      })
    },
    // 处理上传成功后的业务
    handleAvatarSuccess(res, file) {
      console.log(res)
      if (res.code == 200) {
        // 将返回的图片路径复制给directorAvatar
        this.form.directorAvatar = res.data
      }
    },
    // 上传照片之前执行的业务
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg"
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!")
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!")
      }
      return isJPG && isLt2M
    },
  },
}
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 4px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
